package com.ac.api.process_history.service.impl;

import com.ac.api.process_line.entity.ProcessLine;
import com.ac.api.process_line.service.ProcessLineService;
import com.ac.api.process_task.entity.ProcessTask;
import com.ac.api.process_task.service.ProcessTaskService;
import com.ac.api.production_order.entity.ProductionOrder;
import com.ac.api.production_order.service.ProductionOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ac.api.process_history.entity.ProcessHistory;
import com.ac.api.process_history.dao.ProcessHistoryMapper;
import com.ac.api.process_history.service.ProcessHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 工序任务历史记录表 服务实现类
 * </p>
 *
 * @author dc
 * @since 2025-06-11
 */
@Service
public class ProcessHistoryServiceImpl extends ServiceImpl<ProcessHistoryMapper, ProcessHistory> implements ProcessHistoryService {
@Autowired
private ProcessTaskService processTaskService;
@Autowired
private ProcessLineService processLineService;
@Autowired
private ProductionOrderService productionOrderService;
    @Override
    public List<ProcessHistory> list(ProcessHistory example) {
        return this.list(buildWrapper(example));
    }

    @Override
    public IPage<ProcessHistory> page(ProcessHistory example, IPage page) {
        return this.page(page,buildWrapper(example));
    }

    @Override
    @Transactional
    public void addHistory(Integer id, Integer lineId, String status) {
        ProductionOrder productionOrder = productionOrderService.getById( id);
        ProcessLine processline = processLineService.getById(lineId);
            ProcessHistory processHistory = new ProcessHistory();
            processHistory.setTaskId(processline.getLineId());
            processHistory.setProcessName(processline.getProductModel());
            processHistory.setStartTime(processline.getStartTime());
            processHistory.setEndTime(LocalDateTime.now());
            processHistory.setYieldRate(calculateYieldRate(productionOrder));
            processHistory.setStatus(status);
            processHistory.setRecordTime(LocalDateTime.now());
            this.save(processHistory);
    }
    // 安全计算良品率
    public double calculateYieldRate(ProductionOrder order) {
        if (order.getCount() == 0) {
            return 0.0f; // 默认值
        }
        // 使用 double 提高精度
        return  (1 - ((double) order.getLowcount() / (double)order.getCount()));
    }
    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<ProcessHistory> buildWrapper(ProcessHistory example) {
        QueryWrapper<ProcessHistory> wrapper     = new QueryWrapper<>();
        return wrapper;
    }

}
